package com.rules.admin.utils;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import org.springside.modules.utils.mapper.JsonMapper;


public class JsonMapperHolder {
    public static final ObjectMapper objectMapper;

    public static final JsonMapper jsonMapper = JsonMapper.nonEmptyMapper();

    public JsonMapperHolder() {
    }

    public static final ObjectNode createObjectNode() {
        return objectMapper.createObjectNode();
    }

    public static final <T> T convert(Object value, Class<T> clazz) {
        return objectMapper.convertValue(value, clazz);
    }

    public static final JsonNode convert(Object value) {
        return (JsonNode)convert(value, JsonNode.class);
    }

    static {
        objectMapper = jsonMapper.getMapper();
        objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
        objectMapper.setDateFormat(new ISO8601DateFormat());
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    }
}
